package com.ruix.app.util;

import android.content.Context;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;

import com.hyphenate.EMError;
import com.hyphenate.chat.EMClient;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * Created by hadexs on 2017/6/19.
 */

public class VoiceRecorder {
    MediaRecorder recorder;

    static final String PREFIX = "voice";
    static final String EXTENSION = ".amr";

    public static boolean isRecording = false;
    private long startTime;
    private String voiceFile = null;
    private String voiceFilePath = null;
    private String voiceFileName = null;
    private File file;

    /**
     * start recording to the file
     */
    public String startRecording() {
        file = null;
        try {
            // need to create recorder every time, otherwise, will got exception
            // from setOutputFile when try to reuse
            if (recorder != null) {
                recorder.release();
                recorder = null;
            }
            recorder = new MediaRecorder();
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setAudioChannels(1); // MONO
            recorder.setAudioSamplingRate(8000); // 8000Hz
            recorder.setAudioEncodingBitRate(64); // seems if change this to
            // 128, still got same file
            // size.
            // one easy way is to use temp file
            // file = File.createTempFile(PREFIX + userId, EXTENSION,
//             User.getVoicePath());
            voiceFileName = getVoiceFileName(EMClient.getInstance().getCurrentUser());
            voiceFilePath = voiceFile + "/" + voiceFileName;
            file = new File(voiceFilePath);
            recorder.setOutputFile(file.getAbsolutePath());
            recorder.prepare();
            recorder.start();
            isRecording = true;
        } catch (IOException e) {
            e.printStackTrace();
//            EMLog.e("voice", "prepare() failed");
        }
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    while (isRecording) {
//                        android.os.Message msg = new android.os.Message();
//                        msg.what = recorder.getMaxAmplitude() * 13 / 0x7FFF;
//                        handler.sendMessage(msg);
//                        SystemClock.sleep(100);
//                    }
//                } catch (Exception e) {
//                    // from the crash report website, found one NPE crash from
//                    // one android 4.0.4 htc phone
//                    // maybe handler is null for some reason
//                    EMLog.e("voice", e.toString());
//                }
//            }
//        }).start();
        startTime = new Date().getTime();
//        EMLog.d("voice", "start voice recording to file:" + file.getAbsolutePath());
        return file == null ? null : file.getAbsolutePath();
    }

    /**
     * stop the recoding
     *
     * @return seconds of the voice recorded
     */

    public void discardRecording() {
        if (recorder != null) {
            try {
                recorder.stop();
                recorder.release();
                recorder = null;
                if (file != null && file.exists() && !file.isDirectory()) {
                    file.delete();
                }
            } catch (IllegalStateException e) {

            } catch (RuntimeException e){

            }
            isRecording = false;
        }
    }

    public int stopRecoding() {
        if(recorder != null){
            isRecording = false;
            recorder.stop();
            recorder.release();
            recorder = null;

            if(file == null || !file.exists() || !file.isFile()){
                return EMError.FILE_INVALID;
            }
            if (file.length() == 0) {
                file.delete();
                return EMError.FILE_INVALID;
            }
            int seconds = (int) (new Date().getTime() - startTime) / 1000;
//            EMLog.d("voice", "voice recording finished. seconds:" + seconds + " file length:" + file.length());
            return seconds;
        }
        return 0;
    }

    protected void finalize() throws Throwable {
        super.finalize();
        if (recorder != null) {
            recorder.release();
        }
    }

    private String getVoiceFileName(String uid) {
        return uid + System.currentTimeMillis() + EXTENSION;
    }

    public boolean isRecording() {
        return isRecording;
    }


    public String getVoiceFilePath() {
        return voiceFilePath;
    }

    public String getVoiceFileName() {
        return voiceFileName;
    }

    public void initPath(Context context){
        File voicePath =  new File(getStorageDir(context), "voice");
        if (!voicePath.exists()) {
            voicePath.mkdirs();
        }
        voiceFile = voicePath.getAbsolutePath();
    }

    private File storageDir = null;

    private File getStorageDir(Context applicationContext) {
        if (storageDir == null) {
            //try to use sd card if possible
            File sdPath = Environment.getExternalStorageDirectory();
            if (sdPath.exists()) {
                return sdPath;
            }
            //use application internal storage instead
            storageDir = applicationContext.getFilesDir();
        }
        return storageDir;
    }
}
